﻿using System;
using System.Collections.Generic;
//using System.Linq;
using System.Text;
//using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Windows.Forms;
using System.Management;
using System.Net;
using System.Net.Sockets;

namespace LabelPrintSystem
{
    class ClsQueryResult
    {//查询测试交调、电性能、特采结果
        private const string pass = "PASS";
        private const string TestStep = "成品%";
        private const string m_sConnect = "ANTENNA";//连接数据类型
        public ClsDataProduct ClsDataProductParam;//天线产品信息
        private string m_sError;//错误
        private string m_sProductSN;//天线条码
        public ClsQueryResult(string sProductSN)
        {//构造
            m_sProductSN = sProductSN.ToUpper();
        }
        public string error()
        {//反馈错误
            return m_sError;
        }
        public bool matchSN()
        {//匹配产品信息
            if (m_sProductSN == null || m_sProductSN == "") return false;
            try
            {
                //            select MOID from THouse where ProductSN like 'mbtda0100324h07%';
                //            select ProductTypeID from TMO where MOID = 'MO-1702090183';
                //            select ProductTypeName from TProductType where ProductTypeID = 'TDA0100324';
                ClsDataProductParam = new ClsDataProduct();
                ClsDataProductParam.sProductSN = m_sProductSN;
                string sSql = "select MOID from THouse where ProductSN = '" + ClsDataProductParam.sProductSN + "';";//产品批次MOID
                ClsDataProductParam.sMOID = MicroDBHelper.ExecuteScalar(sSql, null, m_sConnect) as string;
                if (ClsDataProductParam.sMOID == null || ClsDataProductParam.sMOID == "")
                {
                    m_sError = "该条码未注册，无法使用";
                    return false;
                }

                sSql = "select ProductTypeID from TMO where MOID = '" + ClsDataProductParam.sMOID + "';";//产品代码
                ClsDataProductParam.sProcode = MicroDBHelper.ExecuteScalar(sSql, null, m_sConnect) as string;
                if (ClsDataProductParam.sProcode == null || ClsDataProductParam.sProcode == "")
                {
                    m_sError = "该条码未注册，无法使用";
                    return false;
                }

                sSql = "select ProductTypeName from TProductType where ProductTypeID = '" + ClsDataProductParam.sProcode + "';";//产品名称
                ClsDataProductParam.sProcodeName = MicroDBHelper.ExecuteScalar(sSql, null, m_sConnect) as string;
                if (ClsDataProductParam.sProcodeName == null || ClsDataProductParam.sProcodeName == "")
                {
                    m_sError = "该条码未注册，无法使用";
                    return false;
                }

                return true;
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message);
            }

            return false;
        }

        public bool isPIMPass(out string MaxPIMValue, string TableName = "[PIMTest]")
        {//成品是否测试过交调
            MaxPIMValue = "0";
            if (m_sProductSN == null || m_sProductSN == "") return false;
            bool value = false;
            try
            {
                string SqlStr = string.Format("select count(*) from {0} where [ProductSN]='{1}' and [PIMTestStep] like '{2}'", TableName, m_sProductSN, TestStep);//该产品代码--成品是否测试过
                int n = (int)MicroDBHelper.ExecuteScalar(SqlStr, null, m_sConnect);
                if (n > 0)
                {//成品测试过
                    List<SqlParameter> lstParam = new List<SqlParameter>();
                    lstParam.Add(new SqlParameter("@ProductSN", m_sProductSN));
                    lstParam.Add(new SqlParameter("@PIMTestStep", TestStep));
                    SqlStr = "select [PIMPass],[MaxPIMValue] from " + TableName + " where [ProductSN] = @ProductSN and [PIMTestStep] like @PIMTestStep order by [PIMRecordTime] desc ";
                    DataTable dtPIMPass = MicroDBHelper.ExecuteDataTable(SqlStr, lstParam.ToArray(), m_sConnect, CommandType.Text);
                    for (int i = 0; i < dtPIMPass.Rows.Count; i++)
                    {
                        MaxPIMValue = dtPIMPass.Rows[i][1].ToString();//交调测试最差值
                        if (dtPIMPass.Rows[i][0].ToString().ToUpper().Contains(pass))
                        {
                            value = true;
                            return value;
                        }

                        if (i == 0)
                        {//只要最近测试的
                            return value;
                        }
                    }
                }
                else
                {//成品未测试过
                    return value;
                }
            }
            catch (Exception ex) { System.Windows.Forms.MessageBox.Show(ex.Message); return false; }
            return value;
        }

        public ClsDataSpecial downladSpecial(string TABLE_NAME = "[tb_MOIDConfig]")
        {//根据MOID获取特采配置
            if (ClsDataProductParam == null) { return null; }
            if (ClsDataProductParam.sMOID == null || ClsDataProductParam.sMOID == "") { return null; }

            try
            {//依据MOID获取特采信息
                ClsDataSpecial ret;
                List<SqlParameter> lstParam = new List<SqlParameter>();
                lstParam.Add(new SqlParameter("@id", ClsDataProductParam.sMOID));

                string sSql = "select [Settings] from " + TABLE_NAME + " where [MOID] = @id";
                string sXml = MicroDBHelper.ExecuteScalar(sSql, lstParam.ToArray(), m_sConnect) as string;
                if (sXml == null || sXml == "")
                {//未发现特采，初始化无效特采
                    ret = new ClsDataSpecial();
                    ret.bEnable = false;
                }
                else
                {
                    ret = (ClsDataSpecial)XmlUtil.XmlTo(sXml, typeof(ClsDataSpecial));//XML反序列化获取对应特采类的数据
                }
                return ret;
            }
            catch (Exception ex) { System.Windows.Forms.MessageBox.Show(ex.Message); return null; }
        }

        public bool isElectricPass(out string MaxVSWR, out string MaxISO, string TableName = "[TTest]")
        {//成品是否测试过电性能
            MaxVSWR = "0"; MaxISO = "0";
            if (m_sProductSN == null || m_sProductSN == "") return false;
            bool value = false;

            try
            {
                string SqlStr = string.Format("select count(*) from {0} where [ProductSN]='{1}' and [TestStep] like '{2}'", TableName, m_sProductSN, TestStep);//该产品代码--成品是否测试过
                int n = (int)MicroDBHelper.ExecuteScalar(SqlStr, null, m_sConnect);
                if (n > 0)
                {//成品测试过
                    List<SqlParameter> lstParam = new List<SqlParameter>();
                    lstParam.Add(new SqlParameter("@ProductSN", m_sProductSN));
                    lstParam.Add(new SqlParameter("@TestStep", TestStep));
                    SqlStr = "select [IsPass],[MaxVSWR],[MaxISO]from " + TableName + " where [ProductSN] = @ProductSN and [TestStep] like @TestStep order by [RecordTime] desc ";
                    DataTable dtPIMPass = MicroDBHelper.ExecuteDataTable(SqlStr, lstParam.ToArray(), m_sConnect, CommandType.Text);
                    for (int i = 0; i < dtPIMPass.Rows.Count; i++)
                    {
                        MaxVSWR = dtPIMPass.Rows[i][1].ToString();//驻波测试最差值
                        MaxISO = dtPIMPass.Rows[i][2].ToString();//隔离测试最差值
                        if (dtPIMPass.Rows[i][0].ToString().ToUpper().Contains(pass))
                        {
                            value = true;
                            return value;
                        }

                        if (i == 0)
                        {//只要最近测试的
                            return value;
                        }
                    }
                }
                else
                {//成品未测试过
                    return value;
                }
            }
            catch (Exception ex) { System.Windows.Forms.MessageBox.Show(ex.Message); return false; }
            return value;
        }

        public bool isRCUStatuePass(string TableName = "[tb_RCUStatusCheck]")
        {//RCU检测是否合格
            if (m_sProductSN == null || m_sProductSN == "") return false;
            bool value = false;
            try
            {
                string SqlStr = string.Format("select count(*) from {0} where [ProductSN]='{1}' ", TableName, m_sProductSN);//该产品代码--RCU是否测试过
                int n = (int)MicroDBHelper.ExecuteScalar(SqlStr, null, m_sConnect);
                if (n > 0)
                {//RCU测试过
                    List<SqlParameter> lstParam = new List<SqlParameter>();
                    lstParam.Add(new SqlParameter("@ProductSN", m_sProductSN));
                    SqlStr = "select [Status] from " + TableName + " where [ProductSN] = @ProductSN order by [time] desc ";
                    DataTable dtPIMPass = MicroDBHelper.ExecuteDataTable(SqlStr, lstParam.ToArray(), m_sConnect, CommandType.Text);
                    for (int i = 0; i < dtPIMPass.Rows.Count; i++)
                    {
                        if (dtPIMPass.Rows[i][0].ToString().ToUpper().Contains(pass))
                        {
                            value = true;
                            return value;
                        }

                        if (i == 0)
                        {//只要最近测试的
                            return value;
                        }
                    }
                }
                else
                {//RCU未测试过
                    return value;
                }
            }
            catch (Exception ex) { System.Windows.Forms.MessageBox.Show(ex.Message); return false; }
            return value;
        }

    }
   
}
